package com.ky.serialport.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.ky.serialport.message.MessageEvent;
import com.ky.serialport.utils.FormatUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 用于生成模拟数据
 */
public class SimulateService extends Service {

    private boolean isRun = false;
    private String TAG = "SimulateService";
    private SendDataThread sendDataThread;

    @Override
    public void onCreate() {
        super.onCreate();
        FormatUtils.init();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void getEvent(MessageEvent event) {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ===================================");
        if (isRun == false){
            Log.d(TAG, "onStartCommand: 启动线程");
            isRun = true;
            sendDataThread = new SendDataThread();
            sendDataThread.start();
        }

        return super.onStartCommand(intent, flags, startId);
    }

    public class SendDataThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (isRun && !isInterrupted()) {
                Log.d(TAG, "run: 发送数据...............................");
//                EventBus.getDefault().post(new WenShiDuDataBean().test());
//                EventBus.getDefault().post(new JiGuangCejuBean().test());
//                EventBus.getDefault().post(new LaliBean().test());
//                EventBus.getDefault().post(new ShouShaLiBean().test());
//                EventBus.getDefault().post(new TaBanLiBean().test());
//                EventBus.getDefault().post(new FxpBean().test());
//                EventBus.getDefault().post(new YaLiBean().test());
//                EventBus.getDefault().post(new QingJiaoBean().test());
//                EventBus.getDefault().post(new SpeedBean().test());
//                EventBus.getDefault().post(new GPSBean().test());
//                EventBus.getDefault().post(new WeiQiBean().test());
                try {
                    Thread.sleep(50);//显示性能高的话，可以把此数值调小。
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        isRun = false;
        if (sendDataThread!=null){
            sendDataThread = null;
        }
        super.onDestroy();
    }

}
